if (Ext.grid.RowEditor) {
    Ext.apply(Ext.grid.RowEditor.prototype, {
        saveBtnText: '保存',
        cancelBtnText: '取消',
        errorsText: '错误信息',
        dirtyText: '已修改,你需要提交或取消变更'
    });
}

Ext.define('Writer.Grid', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.writergrid',

    requires: [
        'Ext.grid.plugin.CellEditing',
        'Ext.form.field.Text',
        'Ext.toolbar.TextItem'
    ],

    initComponent: function() {
        this.addEvents('rights');
        this.editing = Ext.create('Ext.grid.plugin.RowEditing');

        Ext.apply(this, {
            iconCls: 'icon-grid',
            dwid: null, //单位ID
            userid: null,
            frame: true,
            plugins: [this.editing],
            dockedItems: [{
                xtype: 'toolbar',
                items: [{
                    iconCls: 'icon-add',
                    text: '增加',
                    scope: this,
                    handler: this.onAddClick
                }, {
                    iconCls: 'icon-delete',
                    text: '删除',
                    disabled: true,
                    itemId: 'delete',
                    scope: this,
                    handler: this.onDeleteClick
                }, {
                    iconCls: 'icon-user',
                    text: '授权',
                    disabled: true,
                    itemId: 'rights',
                    scope: this,
                    handler: this.onRightsClick
}]
}],
                    columns: [{
                        header: '账号',
                        width: 100,
                        sortable: true,
                        dataIndex: 'LoginID',
                        field: {
                            type: 'textfield'
                        }
                    }, {
                        header: '姓名',
                        width: 100,
                        sortable: true,
                        dataIndex: 'UserName',
                        field: {
                            type: 'textfield'
                        }
                    }, {
                        header: '口令',
                        width: 100,
                        sortable: true,
                        dataIndex: 'pwd',
                        field: {
                            type: 'textfield'
                        }
                    }, {
                        header: '邮箱',
                        flex: 1,
                        sortable: true,
                        dataIndex: 'mail',
                        field: {
                            type: 'textfield'
                        }
}]
                    });
                    this.callParent();
                    this.getSelectionModel().on('selectionchange', this.onSelectChange, this);
                },

                onSelectChange: function(selModel, selections) {
                    this.down('#delete').setDisabled(selections.length === 0);
                    this.down('#rights').setDisabled(selections.length === 0);
                    if (selections.length > 0)
                        this.userid = selections[0].getId();
                },

                onSync: function() {
                    this.store.sync();
                },

                onRightsClick: function() {
                    var selection = this.getView().getSelectionModel().getSelection()[0];
                    if (selection) {
                        var id = selection.getId();// showGroupForm(id);
                        this.fireEvent('rights', this, id);
                    }
                },
                onDeleteClick: function() {
                    var selection = this.getView().getSelectionModel().getSelection()[0];
                    if (selection) {
                        this.store.remove(selection);
                    }
                },

                onAddClick: function() {
                    if (!this.dwid) return;
                    var rec = new Writer.Person({
                        dwID: this.dwid
                    })
                    this.store.insert(0, rec);
                    this.editing.startEdit(0, 0);
                }
            });

            Ext.define('Writer.Person', {
                extend: 'Ext.data.Model',
                fields: [{
                    name: 'id',
                    useNull: true
                }, 'mail', 'UserName', 'LoginID', 'dwID', 'isadmin', 'IsUse','pwd'],
                validations: [{
                    type: 'length',
                    field: 'mail',
                    min: 0
                }, {
                    type: 'length',
                    field: 'UserName',
                    min: 1
                }, {
                    type: 'length',
                    field: 'LoginID',
                    min: 1
}]
                });
                Ext.require([
    'Ext.data.*',
    'Ext.tip.QuickTipManager',
    'Ext.window.MessageBox', 'Writer.Grid'
]);

                Ext.onReady(function() {
                    Ext.tip.QuickTipManager.init();
                    var dwid;
                    var treestore = Ext.create('Ext.data.TreeStore', {
                        proxy: {
                            type: 'ajax',
                            url: 'yhpcOrgTreeAjax.aspx'
                        },
                        root: {
                            id: 'root',
                            expanded: true
                        }
                    });
                    var treePanel = Ext.create('Ext.tree.Panel', {
                        id: 'tree-panel',
                        title: '单位分类',
                        region: 'west',
                        split: true,
                        width: 250,
                        minSize: 150,
                        rootVisible: false,
                        collapsible: true,
                        floatable: true,
                        autoScroll: true,
                        store: treestore
                    });

                    treePanel.getSelectionModel().on('select', function(selModel, record) {
                        if (record) {
                            store.clearFilter(false);
                            dwid = record.getId();
                            grid.dwid = dwid;
                            var params = { params: { dwid: dwid} };
                            //store.filter("dwID", dwid);
                            store.load(params);
                        }
                    });
                    var store = Ext.create('Ext.data.Store', {
                        model: 'Writer.Person',
                        //autoLoad: true,
                        autoSync: true,
                        proxy: {
                            type: 'ajax',
                            api: {
                                read: 'sysUserAjax.aspx?action=read',
                                create: 'sysUserAjax.aspx?action=create',
                                update: 'sysUserAjax.aspx?action=update',
                                destroy: 'sysUserAjax.aspx?action=delete'
                            },
                            reader: {
                                type: 'json',
                                successProperty: 'success',
                                root: 'data',
                                messageProperty: 'message'
                            },
                            writer: {
                                type: 'json',
                                //writeAllFields: false,
                                root: 'data'
                            },
                            listeners: {
                                exception: function(proxy, response, operation) {
                                    Ext.MessageBox.show({
                                        title: '保存失败',
                                        msg: operation.getError(),
                                        icon: Ext.MessageBox.ERROR,
                                        buttons: Ext.Msg.OK
                                    });
                                }
                            }
                        },
                        listeners: {
                            write: function(proxy, operation) {
                                if (operation.action == 'destroy') {
                                    //main.child('#form').setActiveRecord(null);
                                }
                                //this.msg(operation.action, operation.resultSet.message);
                            }
                        }

                    });
                    var grid = Ext.create('Writer.Grid', {
                        region: 'center',
                        store: store,
                        title: '用户列表',
                        listeners: {
                            rights: function(form, data) {
                            showGroupForm(data);
                            }
                        }
                    });
                    Ext.create('Ext.Viewport', {
                        layout: {
                            type: 'border',
                            padding: 5
                        },
                        defaults: {
                            split: true
                        },
                        items: [treePanel,
                        grid
                        ]
                    });
                });
